package ai4games_tests.tests;

import game2dai.Domain;
import game2dai.World;
import game2dai.entities.Building;
import game2dai.entities.Vehicle;
import game2dai.entityshapes.ps.BuildingPic;
import game2dai.entityshapes.ps.PersonPic;
import game2dai.maths.Vector2D;
import game2dai.steering.AutoPilot;
import game2dai.utils.StopWatch;
import processing.core.PApplet;

public class WallAvoidanceTest extends PApplet {

	World w;
	Domain wd;
	PersonPic apic;

	StopWatch sw = new StopWatch();

	public void setup(){
		size(400,400);
		background(255);
		noLoop();
		// Create the world first
		w = new World(400,300);
		w.noOverlap(true);
		w.scale(0.8);
		wd = new Domain(0,0,400,300);

		createBuildings();
		redraw();
		createMovers();
		loop();
		// Last thing in setup is to reset the timer
		sw.reset();
	}

	public void draw(){
		w.update(sw.getElapsedTime());
		background(255);

		pushMatrix();
		translate((float)w.xOffset(), (float)(w.yOffset()));
		scale((float) w.scale());
		w.draw();
		popMatrix();
	}

	public void createMovers(){
		// Now create the touristSB sb = new SteeringBehaviour
		AutoPilot sb = new AutoPilot();
		//sb.enableBehaviours(SBF.WANDER | SBF.WALL_AVOID);
		sb.wanderOn().wallAvoidOn();
//		sb.setWanderDetails(200, 50, 100);
		
		
		sb.wanderFactors(100, 50, 200);
		
		apic = new PersonPic(this, 6, color(255,160,255), color(145,64,47), color(0), 1);

		int count = 10;
		int col = color(255);
		int x, y, c;
		Vector2D pos, vel;

		while(count > 0){
			do {
				x = (int) random(400);
				y = (int) random(300);
				c = get(x,y);
			} while(c != col);
			pos = new Vector2D(x,y);
			float a = random(TWO_PI);
			vel = new Vector2D(20*cos(a), 20*sin(a));

			Vehicle v = new Vehicle(new Vector2D(x, y), // position
					6, // collision radius
					new Vector2D(20*cos(a), 20*sin(a)), // velocity
					40, // maximum speed
					new Vector2D(cos(a), sin(a)), // heading
					1, // mass
					2,  // turning rate
					500 // max force
			); 
			v.renderer(apic);
			v.worldDomain(wd);
			v.AP((AutoPilot)sb.clone());
			w.add(v);
			count--;
		}
	}

	public void createBuildings() {
		Building b;
		Vector2D[] c;
		int fillCol = color(32, 128, 128);
		int edgeCol = color(64,200,64);

		BuildingPic bpic = new BuildingPic(this, fillCol, edgeCol, 2.0f);

		// Building A
		c = new Vector2D[] { new Vector2D(0,0), new Vector2D(0,50),
				new Vector2D(150,50), new Vector2D(200,20),
				new Vector2D(200,0)
		};
		b = new Building(c);
		b.renderer(bpic);
		w.add(b);

		// Building B
		c = new Vector2D[] { new Vector2D(250,0), new Vector2D(250,20),
				new Vector2D(300,76), new Vector2D(350,50),
				new Vector2D(400,50), new Vector2D(400,0)
		};
		b = new Building(c);
		b.renderer(bpic);
		w.add(b);

		// Building C
		c = new Vector2D[] { new Vector2D(0,100), new Vector2D(0,300),
				new Vector2D(200,300), new Vector2D(200,250),
				new Vector2D(10,250), new Vector2D(10,100)
		};
		b = new Building(c);
		b.renderer(bpic);
		w.add(b);

		// Building D
		c = new Vector2D[] { new Vector2D(90,100), new Vector2D(90,200),
				new Vector2D(200,200)
		};
		b = new Building(c);
		b.renderer(bpic);
		w.add(b);

		// Building E
		c = new Vector2D[] { new Vector2D(150,100), new Vector2D(250,190),
				new Vector2D(250,100), new Vector2D(210,70)
		};
		b = new Building(c);
		b.renderer(bpic);
		w.add(b);

		// Building F
		c = new Vector2D[] { new Vector2D(400,100), new Vector2D(370,100),
				new Vector2D(300,140), new Vector2D(300,230),
				new Vector2D(250,250), new Vector2D(250,300),
				new Vector2D(400,300)
		};
		b = new Building(c);
		b.renderer(bpic);
		w.add(b);
	}

}
